package com.ucar.notice.notice.handler;

import com.ucar.base.vo.OpResult;
import com.ucar.notice.notice.handler.callback.InvokeMessageCallback;
import com.ucar.notice.notice.handler.callback.IssueProcessCallback;
import com.ucar.notice.notice.pojo.NoticeBean;
import com.ucar.notice.notice.service.NoticeIssueService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Description: 抽象下发司机通知处理类<br/>
 * @version V1.0  2017/5/30 21:31  by 石冬冬(dongdong.shi@ucarinc.com) - Heil Hitler
 */
@Service
public abstract class AbstractNoticeIssueProcessor implements NoticeIssueProcess {
    public final Logger logger = LoggerFactory.getLogger(AbstractNoticeIssueProcessor.class);
    @Autowired
    private NoticeIssueService noticeIssueService;
    /**
     * 司机通知集合
     */
    private List<NoticeBean> noticeList;
    /**
     * 操作结果
     */
    private OpResult<String> opResult;
    /**
     * 对数据进行过滤筛选的钩子方法
     * @return
     */
    protected abstract void filterDatas();
    /**
     * 对数据进行校验的钩子方法
     * @return
     */
    protected abstract OpResult<String> checkDatas(InvokeMessageCallback messageCallback);

    @Override
    public OpResult<String> handle(IssueProcessCallback callback) {
        this.noticeList = callback.queryList();
        this.filterDatas();
        this.opResult = this.checkDatas(callback.call());
        if(this.opResult.isSuccess()){
            this.noticeIssueService.execute(this.noticeList);
            this.after();
        }
        return this.opResult;
    }

    /**
     * 处理完毕后置处理，包括
     * 1、更新司机通知状态
     * 2、保存通知历史记录
     */
    protected abstract void after();

    public List<NoticeBean> getNoticeList() {
        return noticeList;
    }
}
